﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WROA.Models.ViewModels;
using WROA.Models.Services;
using WROA.Models.Entitys;
using WROA.Models.IServices;

namespace WROA.Controllers
{
    public class MeetingController : Controller
    {
        //
        // GET: /Meeting/

        /// <summary>
        /// 申请会议
        /// </summary>
        public ActionResult ApplyMeeting()
        {
            ApplyMeetingView model = new ApplyMeetingView();
            return View(model);
        }

        /// <summary>
        /// 申请会议Post
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ApplyMeeting(FormCollection form, HttpPostedFileBase Uploadfile)
        {
            IMeetingService ms = new MeetingService();
            //申请会议室在特定时间内是否空闲
            if (!ms.RoomIsFree(form["RoomName"], Convert.ToDateTime(form["StartTime"]), Convert.ToDateTime(form["EndTime"])))
            {
                ModelState.AddModelError("RoomIsNotFree", "该时间段会议室有会议进行，请重新选择其他时间！");
                return View();
            }
            Dictionary<string, int> equipmentData = new Dictionary<string,int>();//研究下怎么放进来
            foreach (KeyValuePair<string, int> pair in equipmentData)//设备是否够用
            {
                if(!ms.EquipmentIsEnough(Convert.ToDateTime(form["StartTime"]),Convert.ToDateTime(form["EndTime"]),ms.GetEquipment(pair.Key),pair.Value))
                {
                    ModelState.AddModelError("EquipmentIsNotEnough", "该时间段空闲"+ pair.Key +"数量不足，请减少数量！");
                    return View();
                }
            }
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            
            ApplyMeeting applyMeeting = new ApplyMeeting();
            applyMeeting.Am_time = DateTime.Now;
            applyMeeting.User = us.GetUser(User.Identity.Name);
            applyMeeting.Flow = fs.GetFlow(form["FlowName"]);//根据流程名得到Flow对象
            applyMeeting.Procedure = fs.GetFirstProcedure(applyMeeting.Flow);//获取流程第一个步骤
            applyMeeting.Am_htmldata = form["Html"];
            applyMeeting.Am_state = "未结束";
            applyMeeting.Am_isagree = null;
            //applyMeeting.U_name = null;
            if (Uploadfile != null) //如果有上传附件
            {
                Uploadfile.SaveAs(Server.MapPath("~/UpLoadFile/MeetingFile/") + Uploadfile.FileName);//保存到服务器
                //保存到数据库
                Models.Entitys.File file = new Models.Entitys.File();
                file.User = us.GetUser(User.Identity.Name);
                file.File_name = Uploadfile.FileName;
                file.File_size = Uploadfile.ContentLength.ToString();
                file.File_date = DateTime.Now;
                file.File_path = Server.MapPath("~/UpLoadFile/MeetingFile/");
                fs.Save(file);

                applyMeeting.File = file;
            }
            else//如果没有上传附件
            {
                applyMeeting.File = null;
            }
            ms.Save(applyMeeting);

            //保存会议信息
            MeetingData meetingData = new MeetingData();
            meetingData.Applymeeting = applyMeeting;
            meetingData.Meetingtype = ms.GetMeetingType(form["MeetingType"]);
            meetingData.Md_topic = form["Topic"];
            meetingData.Md_intro = form["Intro"];
            meetingData.Md_starttime = Convert.ToDateTime(form["StartTime"]);
            meetingData.Md_endtime = Convert.ToDateTime(form["EndTime"]);
            meetingData.Md_state = "未开始";
            ms.Save(meetingData);

            //保存会议细节信息到各个表
            List<string> username = new List<string>();//研究下如何放进来
            Room room = ms.GetRoom(form["RoomName"]);
            ms.SaveMeetingData(meetingData, username, room, equipmentData);

            return View();//????
        }

    }
}
